package com.up.jiasai.spring.resolver;

import com.up.vms.application.base.auth.AdminSession;
import com.up.vms.application.base.auth.UserSession;
import com.up.vms.application.base.exception.SysException;
import com.up.vms.application.base.utils.LogUtils;
import com.up.jiasai.cache.CacheService;
import com.up.jiasai.spring.SpringContext;
import com.up.jiasai.spring.annotation.LoginAuth;
import com.up.jiasai.spring.annotation.LoginedAuth;
import com.up.vms.application.common.utils.StringUtils;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import java.lang.annotation.Annotation;

import javax.servlet.http.HttpServletRequest;

/**
 * 登陆解析器
 * 功能说明：<br>
 * 模块名称：<br>
 * 功能描述：LoginedArgumentResolver<br>
 * 文件名称: LoginedArgumentResolver.java<br>
 * 系统名称：ICELOVE<br>
 * 软件著作权：ICELOVE 版权所有<br>
 * 开发人员：lujun <br>
 * 开发时间：2016-8-29 下午9:56:54<br>
 * 系统版本：1.0.0<br>
 */
public class LoginArgumentResolver implements HandlerMethodArgumentResolver {
    private CacheService cacheService;

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        //LogUtils.error(this, "LoginArgumentResolver>>>>");
        //加快性能判断,对于那些没有注释的接口直接忽略
        if (!parameter.hasParameterAnnotations()) return false;
        //如果该参数注解有@LoginAuth
        boolean find = false;
        Annotation[] anns = parameter.getParameterAnnotations();
        for (Annotation ann : anns) {
            if (LoginAuth.class.isInstance(ann) || LoginedAuth.class.isInstance(ann)) {
                find = true;
                break;
            }
        }
        if (!find) return false;
        //如果该参数的类型为UserSession || AdminSession 返回true表示可以处理下一步
        Class<?> type = parameter.getParameterType();
        if (type == UserSession.class || type == AdminSession.class) return true;
        return false;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
        //这里暂时把User对象放在session中
        //这里最好用Session来做,不建议用memcached来做登陆信息保存(目前方便起见还是缓存获取吧)
        Class<?> type = parameter.getParameterType();
        if (type == UserSession.class) {
            String openId = (String) request.getAttribute("openId");
            LogUtils.error(this, "resolveArgument的openId=" + openId);
            if (StringUtils.isEmpty(openId)) {
                if (parameter.hasParameterAnnotation(LoginedAuth.class)) {
                    throw new SysException("请登录");
                }
            }
            UserSession userCache = null;
            if (StringUtils.isNotEmpty(openId)) {
                if (cacheService == null) {
                    cacheService = SpringContext.getBean("cacheService", CacheService.class);
                }
                userCache = cacheService.getUserSession(openId);
            }
            if (userCache == null) {
                //必须登录
                if (parameter.hasParameterAnnotation(LoginedAuth.class)) {
                    throw new SysException("请登录");
                }
            }
            return userCache;
//        } else if (type == AdminSession.class) {
//            AdminSession adminSession = (AdminSession) request.getAttribute(Constants.ADMIN_SESSION_KEY);
//            //必须登录
//            if (parameter.hasParameterAnnotation(LoginedAuth.class) && adminSession == null) {
//                throw new SysException("请登录");
//            }
//            return adminSession;
        }
        return null;
    }
}
